import Vue from "vue";
import Router from "vue-router";
import store from '../store/index';
import { Toast } from 'vant';

Vue.use(Router);

const router = new Router({
  routes: [{
    //根路由
    path: "/",
    redirect: "/home"
  },
  {
    // 首页
    path: '/home',
    component: () => import('@/views/home')
  },
  {
    // 目的地
    path: '/address',
    component: () => import('@/views/address')
  },
  {
    // 商品
    path: '/goods',
    redirect: '/goods/goodsList',
    component: () => import('@/views/goods'),
    children: [
      {
        path: 'goodsList',
        name: 'goodsList',
        meta: {
          title: "商品列表"
        },
        component: () => import('@/views/goods/goodsList'),
      },
      {
        path: 'goodsDetail',
        meta: {
          title: "商品详情",
          needLogin: true
        },
        component: () => import('@/views/goods/goodsDetail'),
      },
      {
        path: 'chooseDate/:houseId/:minPrice',
        meta: {
          title: "选择日期",
          needLogin: true
        },
        component: () => import('@/views/goods/chooseDate'),
      },
    ]
  },
  {
    // 提交需求
    path: '/submitNeed',
    component: () => import('@/views/submitNeed'),
    meta: {
      title: "提交需求",
      needLogin: true
    }
  },
  {
    // 发现
    path: '/find',
    redirect: '/find/recommend',
    component: () => import('@/views/find'),
    children: [
      {
        path: 'recommend',
        component: () => import('@/views/find/recommend'),
      },
      {
        path: 'quality',
        component: () => import('@/views/find/quality'),
      },
      {
        path: 'activity',
        component: () => import('@/views/find/activity'),
      }
    ]
  },
  {
    // 我的
    path: '/my',
    redirect: '/my/center',
    component: () => import('@/views/my'),
    children: [
      {
        path: 'center',
        component: () => import('@/views/my/center'),
      },
      {
        path: 'set',
        meta: {
          title: "设置",
          needLogin: true
        },
        component: () => import('@/views/my/set'),
      },
      {
        path: 'collect',
        meta: {
          title: "我想去",
          needLogin: true
        },
        component: () => import('@/views/my/collect'),
      },
      {
        path: 'history',
        meta: {
          title: "浏览记录",
          needLogin: true
        },
        component: () => import('@/views/my/history'),
      },
    ]
  },
  {
    // 登录
    path: '/login',
    meta: {
      title: "登录"
    },
    component: () => import('@/views/login')
  },
  {
    // 订单
    path: '/order',
    redirect: '/my/orderList',
    component: () => import('@/views/order'),
    children: [
      {
        path: 'orderList',
        meta: {
          title: "订单列表",
          needLogin: true
        },
        component: () => import('@/views/order/orderList'),
      },
      {
        path: 'orderDetail',
        meta: {
          title: "订单详情",
          needLogin: true
        },
        component: () => import('@/views/order/orderDetail'),
      },
      {
        path: 'writeOrder',
        name: 'writeOrder',
        meta: {
          title: "填写订单",
          needLogin: true
        },
        component: () => import('@/views/order/writeOrder'),
      },
      {
        path: 'orderPay',
        meta: {
          title: "订单支付",
          needLogin: true
        },
        component: () => import('@/views/order/orderPay'),
      },
      {
        path: 'pay',
        meta: {
          title: "支付",
          needLogin: true
        },
        component: () => import('@/views/order/pay'),
      },
    ]
  },
  ]
});

router.beforeEach((to, from, next) => {
  document.title = to.meta.title || '维拉度假'
  if (to.meta.needLogin && !store.state.isLogin) {
    next({
      path: '/login'
    })
    Toast.fail('请先登录');
  } else {
    next()
  }
})



export default router;